package org.jdna.minecraft.tools;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.io.FileUtils;

public class BackupManager {
	private Preferences prefs;
	public BackupManager(Preferences prefs) {
		this.prefs = prefs;
	}
	
	public void backupJarOnly() throws IOException {
		String name = createBackupName("jarbackup") + ".zip";
		File destFile = new File(prefs.getBackupDir(), name);
		Util.zip(prefs.getMinecraftDirAsFile(), destFile, new FileFilter() {
			@Override
			public boolean accept(File pathname) {
				return pathname.getName().equalsIgnoreCase("minecraft.jar");
			}
		});
	}

	public static String createBackupName(String name) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss");
		String postfix = sdf.format(new Date(System.currentTimeMillis()));
		return name + "." + postfix;
	}
	
	public void fullBackup() throws IOException {
		String name = createBackupName("fullbackup") + ".zip";
		File destFile = new File(prefs.getBackupDir(), name);
		Util.zip(prefs.getMinecraftDirAsFile(), destFile);
	}
}
